<cfparam name="User_ID" type="numeric" default="0" />
<cfparam name="returnUI" type="string" default="users" />
<cfscript>
	ui = "user";
	view = "form";
	obj = "user";
	user = request.userGateway.get(User_ID = User_ID);
</cfscript>

<cfif IsNumeric(user.getUser_ID())>

	<cfscript>
		roles = request.Base.getRoles();
	</cfscript>
	
	<script src="/assets/jquery.validator/dist/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/jquery.validator/dist/additional-methods.min.js" type="text/javascript"></script>
		
	<script type="text/javascript" language="javascript">
	<!--
		<cfoutput>
			var userID = #user.getUser_ID()#;
			<!--- var status = "#order.getStatus()#";
			var institutionType = "#order.getInstitution_Type()#";
			var statuses = #SerializeJSON(statuses)#;
			var instituionTypes = #SerializeJSON(institutionTypes)#;
			var productQuantityRules; --->
		</cfoutput>
		var validatorConfig = {
			debug: false,
			rules: {
				User_ID: {required: true, digits: true, min: 0},
				Status: {required: true},
				First_Name: {required: true},
				Last_Name: {required: true},
				Role: {required: true},
				Email: {required: true, email: true},
				Password: {required: true},
				Phone: {required: true, phoneUS: true}		
			},
			messages: {
				User_ID: "Required",
				Status: "Required",
				First_Name: "Required",
				Last_Name: "Required",
				Role: "Required",
				Email: {
					required: "Required",
					email: "Invalid"
				},
				Password: "Required",
				Phone: {
					required: "Required",
					phoneUS: "Invalid - ###-###-####"
				}
			},
			focusCleanup: false,
			errorPlacement: function(error, element) {
				$(element).prev("label").children("span").remove();
				error.appendTo(element.prev("label"));
			},
			errorElement: "span",
			errorClass: "label label-danger",
			validClass: "label label-info",
			highlight: function(element, errorClass, validClass) {
				$(element).prev("label").children("span").removeClass(validClass).addClass(errorClass);
			},
			unhighlight: function(element, errorClass, validClass) {
				$(element).prev("label").children("span").removeClass(errorClass).addClass(validClass);
			}
		};
		$(function() {
			$("#userForm").validate(validatorConfig);
		})
	//-->
	</script>

	<cfform id="userForm" name="userForm" action="#script_name#" role="form"  method="post">
		
		<input type="hidden" name="action" value="saveUser" />
		<cfoutput>
			<input type="hidden" name="ui" value="#returnUI#" />
			<input type="hidden" name="User_ID" value="#user.getUser_ID()#" />
			<div class="row">
				<div class="col-lg-4">
					<cfif IsUserLoggedIn()>
						<div class="form-group">
							<label for="Active">Status<span class="label label-info">Required</span></label>
							<select id="Active" name="Active" class="form-control" required>
								<option value="1"<cfif user.getActive() eq 1> selected</cfif>>Active</option>
								<option value="0"<cfif user.getActive() eq 0> selected</cfif>>Inactive</option>
							</select>
						</div>
					</cfif>
					<div class="form-group">
						<label for="First_Name">First Name<span class="label label-info">Required</span></label>
						<input type="text" id="First_Name" name="First_Name" value="#user.getFirst_Name()#" class="form-control" required />
					</div>
					<div class="form-group">
						<label for="Last_Name">Last Name<span class="label label-info">Required</span></label>
						<input type="text" id="Last_Name" name="Last_Name" value="#user.getLast_Name()#" class="form-control" required />
					</div>
					<div class="form-group">
					<label for="Role">Role</label>
						<select name="Role" class="form-control" required>
							<option value="">Select...</option>
							<cfloop index="r" from="1" to="#ArrayLen(roles)#">
								<option value="#roles[r].value#"<cfif user.getRole() eq roles[r].value> selected</cfif>>#roles[r].text#</option>
							</cfloop>
						</select>
					</div>
					
				</div>
				<div class="col-lg-4">
					<div class="form-group">
						<label for="Email">Email<span class="label label-info">Required</span></label>
						<input type="text" id="Email" name="Email" value="#user.getEmail()#" class="form-control" required />
					</div>
					<div class="form-group">
						<label for="Email">Password<span class="label label-info">Required</span></label>
						<input type="password" id="Password" name="Password" value="#user.getPassword()#" class="form-control" required />
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-lg-8">
								<label for="Phone">Phone<span class="label label-info">Required</span></label>
								<input type="text" class="form-control" id="Phone" name="Phone" value="#user.getPhone()#" placeholder="######-######-########" required  />
							</div>
							<div class="col-lg-4">
								<label for="Extension">Extension</label>
								<input type="text" id="Extension" name="Extension" value="#user.getExtension()#" class="form-control" />
							</div>
						</div>
					</div>
				</div>
				<div class="col-lg-4">
					
				</div>
			</div>
			<div class="row">
				<div class="col-lg-12">
					<div class="well well-sm" style="width: 100%;">
						<a href="?ui=#returnUI#" class="btn btn-info">Cancel</a>
						<button type="submit" id="saveButton" name="saveButton" class="btn btn-info input">Save</button>
					</div>
				</div>
			</div>
		</cfoutput>
	</cfform>

<cfelse>
	<div class="alert alert-info">User not found</div>
</cfif>